package cnsukidayo.com.gitee;

import cnsukidayo.com.gitee.utils.Resources;
import com.google.gson.Gson;
import org.fisco.bcos.sdk.BcosSDK;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.transaction.manager.TransactionProcessorFactory;
import org.fisco.bcos.sdk.transaction.model.dto.CallResponse;

import java.util.ArrayList;

public class BcosSDKTest3 {

    private Gson gson = new Gson();
    // 获取配置文件路径
    public final String configFile = Resources.getResource("config.toml");

    public void testClient() throws Exception {
        // 初始化BcosSDK
        BcosSDK bcosSDK = BcosSDK.build(configFile);
        Client client = bcosSDK.getClient(1);
        CryptoKeyPair keyPair = client.getCryptoSuite().createKeyPair();

        AssembleTransactionProcessor transactionProcessor = null;
        /*
        try {
            transactionProcessor = TransactionProcessorFactory.createAssembleTransactionProcessor(client, keyPair, Resources.getResource("abi"), "");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List<Object> params = new ArrayList<>();
        params.add("金毛3");
        params.add(1680938062);
        params.add("苏州");
        params.add("0x0000000000000000000000000000000000000000");
        params.add("0x0000000000000000000000000000000000000000");
        params.add("哈巴狗");
        try {
            // 调用PetHospital合约
            TransactionResponse transactionResponse = transactionProcessor.sendTransactionAndGetResponseByContractLoader("PetHospital", "0xec9e4672a7aa439334c32daf9bc7b0d6c8e34bb1", "giveBirth", params);
        } catch (ABICodecException e) {
            throw new RuntimeException(e);
        } catch (TransactionBaseException e) {
            throw new RuntimeException(e);
        }
         */
        transactionProcessor = TransactionProcessorFactory.createAssembleTransactionProcessor(client, keyPair, Resources.getResource("abi"), "");
        ArrayList<Object> args = new ArrayList<>();
        args.add(1);
        CallResponse callResponse = transactionProcessor.sendCallByContractLoader("PetContract", "0x85b7050cc79e0011a150c8cb561cb8a451b67658", "getPets", args);
        String value = callResponse.getReturnABIObject().get(0).getListValues().get(6).getStringValue().getValue();
    }

    public static void main(String[] args) throws Exception {
        BcosSDKTest3 bcosSDKTest3 = new BcosSDKTest3();
        bcosSDKTest3.testClient();
    }

}
